package cn.stylefeng.guns.modular.studentmanage.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.fileTag.model.FileTagParam;
import cn.stylefeng.guns.modular.fileTag.model.FileTagResult;
import cn.stylefeng.guns.modular.filerun.model.FileParam;
import cn.stylefeng.guns.modular.filerun.model.FileResult;
import cn.stylefeng.guns.modular.filerun.model.FileUploadResult;
import cn.stylefeng.guns.modular.filerun.service.FileImgService;
import cn.stylefeng.guns.modular.filerun.service.FileRunService;
import cn.stylefeng.guns.modular.studentmanage.model.LeaveUserParam;
import cn.stylefeng.guns.modular.studentmanage.model.LeaveUserResult;
import cn.stylefeng.guns.modular.studentmanage.service.StuManageService;
import cn.stylefeng.guns.sys.modular.system.entity.FilePO;
import cn.stylefeng.guns.sys.modular.system.entity.FileTagPO;
import cn.stylefeng.guns.sys.modular.system.entity.LeaveUserPO;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;


@Controller
@RequestMapping("/stuManage")
public class StuManageController extends BaseController {

    @Autowired
    private StuManageService stuManageService;

    private String PREFIX = "/stuManage";

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/stuManage.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/stuManage_add.html";
    }

    /**
     * 修改页面
     */
    @RequestMapping("/stuManage_update")
    public String deptUpdate() {
        return PREFIX + "/stuManage_edit.html";
    }

    /**
     * 查询列表
     *
     * @author stylefeng
     * @Date 2019-02-18
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(LeaveUserParam leaveUserParam) {
        Page page = LayuiPageFactory.defaultPage();
        List<LeaveUserResult> results = stuManageService.getStudentList(page, leaveUserParam);
        //权限过滤
        //数据与当前登录用户id比较
        List<LeaveUserResult> collects = null;
        List<LeaveUserResult> collects2 = null;
        collects = results.stream().filter(leaveUserResult -> leaveUserResult.getStairApprovalId().equals(LoginContextHolder.getContext().getUserId())).collect(Collectors.toList());
        //如若登录的是院系领导
        if (collects.size()==0) {
            collects2 = results.stream().filter(leaveUserResult -> leaveUserResult.getFoxbaseApprovalId().equals(LoginContextHolder.getContext().getUserId())).collect(Collectors.toList());
            if (collects2.size()==0){
                //校级领导查询全部
                collects2 =results.stream().filter(leaveUserResult -> leaveUserResult.getTertiaryApprovalId().equals(LoginContextHolder.getContext().getUserId())).collect(Collectors.toList());
            }
        }else{
            collects2=collects;
        }
        page.setRecords(collects2);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(LeaveUserPO leaveUserPO) {
        stuManageService.consummateInfo(leaveUserPO);
        boolean b = stuManageService.saveOrUpdate(leaveUserPO);
        if (b) {
            return ResponseData.success(200, "新增成功", null);
        }
        return ResponseData.success(500, "新增失败", null);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/updateItem")
    @ResponseBody
    public ResponseData updateItem(LeaveUserPO leaveUserPO) {
        boolean b = stuManageService.updateById(leaveUserPO);
        if (b) {
            return ResponseData.success(200, "修改成功", null);
        }
        return ResponseData.success(500, "修改失败", null);
    }

    /**
     * 学生详情
     */
    @ResponseBody
    @RequestMapping(value = "/detail")
    public Object detail(Long leaveUserId) {
        LeaveUserPO leaveUser = stuManageService.getById(leaveUserId);
        Map<String, Object> leaveUserMap = BeanUtil.beanToMap(leaveUser);
        return ResponseData.success(leaveUserMap);
    }

    /**
     * 删除记录（删除）
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(LeaveUserParam leaveUserParam) {
        boolean b = stuManageService.removeById(leaveUserParam.getId());
        if (b) {
            return ResponseData.success(200, "删除成功", null);
        }
        return ResponseData.success(500, "删除失败", null);
    }
}


